﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ProjectEdificio
{
    public partial class AddService : Window
    {
        private Servicio servi;
        private Servicio sPrueba;
        private BDManagment bdm = null;


        public AddService(int IDInquilino)
        {
            InitializeComponent();
            this.servi = new Servicio(IDInquilino);
            bdm = BDManagment.getInstance();

            sPrueba = bdm.searchService(IDInquilino);
            if (sPrueba != null)
            {
                if (sPrueba.Agua != 0)
                {
                    this.checkBoxAgua.IsChecked = true;
                    this.textBoxAgua.Text = sPrueba.Agua.ToString();
                }
                if (sPrueba.Gas != 0)
                {
                    this.checkBoxGas.IsChecked = true;
                    this.textBoxGas.Text = sPrueba.Gas.ToString();
                }
                if (sPrueba.Luz != 0)
                {
                    this.checkBoxLuz.IsChecked = true;
                    this.textBoxLuz.Text = sPrueba.Luz.ToString();
                }
            }
        }

        private void rellenarCampos()
        {

        }


        private void buttonCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonAceptar_Click(object sender, RoutedEventArgs e)
        {
            bool flag = false;
            if (checkBoxAgua.IsChecked == true && textBoxAgua.Text != string.Empty)
            {
                servi.Agua = int.Parse(textBoxAgua.Text);
                flag = true;
            }
            if (checkBoxGas.IsChecked == true && textBoxGas.Text != string.Empty)
            {
                servi.Gas = int.Parse(textBoxGas.Text);
                flag = true;
            }
            if (checkBoxLuz.IsChecked == true && textBoxLuz.Text != string.Empty)
            {
                servi.Luz = int.Parse(textBoxLuz.Text);
                flag = true;
            }

            if (checkBoxAgua.IsChecked == true && textBoxAgua.Text == string.Empty)
            {
                MessageBox.Show("Controle los datos");
            }
            if (checkBoxGas.IsChecked == true && textBoxGas.Text == string.Empty)
            {
                MessageBox.Show("Controle los datos");
            }
            if (checkBoxLuz.IsChecked == true && textBoxLuz.Text == string.Empty)
            {
                MessageBox.Show("Controle los datos");
            }

            if (flag)
            {
                if (sPrueba != null)
                {
                    bdm.editService(servi);
                    this.Close();
                }
                else
                {
                    bdm.addService(servi);
                    this.Close();
                }
            }
        }

        private void checkBoxLuz_Checked(object sender, RoutedEventArgs e)
        {
            this.textBoxLuz.IsEnabled = true;
        }

        private void checkBoxLuz_Unchecked(object sender, RoutedEventArgs e)
        {
            this.textBoxLuz.IsEnabled = false;
            this.textBoxLuz.Text = string.Empty;
        }

        private void checkBoxGas_Checked(object sender, RoutedEventArgs e)
        {
            this.textBoxGas.IsEnabled = true;
        }

        private void checkBoxGas_Unchecked(object sender, RoutedEventArgs e)
        {
            this.textBoxGas.IsEnabled = false;
            this.textBoxGas.Text = string.Empty;
        }

        private void checkBoxAgua_Checked(object sender, RoutedEventArgs e)
        {
            this.textBoxAgua.IsEnabled = true;
        }

        private void checkBoxAgua_Unchecked(object sender, RoutedEventArgs e)
        {
            this.textBoxAgua.IsEnabled = false;
            this.textBoxAgua.Text = string.Empty;
        }

        private void numerosTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                e.Handled = false;
            else
                e.Handled = true;
        }

    }
}
